<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" size="mini" @click="delAll"
          >批量删除</el-button
        >
        <el-input
          v-model="select_word"
          size="mini"
          placeholder="请输入村民姓名"
          class="handle-input"
        ></el-input>
        <el-button
          type="primary"
          size="mini"
          @click="centerDialogVisible = true"
          >添加村民留言</el-button
        >
      </div>
    </div>
    <!-- 查看 -->
    <el-table
      size="mini"
      ref="multipleTable"
      border
      style="width: 100%"
      height="570px"
      :data="data"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column
        prop="villageName"
        label="村民姓名"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="townName"
        label="村镇姓名"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="cadreName"
        label="干部姓名"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="time"
        label="留言时间"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column label="留言状态" width="120" align="center">
        <template slot-scope="scope">
          {{ changeStatus(scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="messageContent"
        label="留言内容"
        width="300"
        align="center"
      ></el-table-column>
      <el-table-column label="解决状态" width="120" align="center">
        <template slot-scope="scope">
          {{ changeResolutionStatus(scope.row.resolutionStatus) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="typeOfDifficulty"
        label="困难类型"
        width="120"
        align="center"
      >
      </el-table-column>

      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 翻页 -->
    <div class="pagination">
      <el-pagination
        background
        layout="total,prev,pager,next"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="tableData.length"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>

    <!-- 添加 -->
    <el-dialog
      title="添加村民留言"
      :visible.sync="centerDialogVisible"
      width="400px"
      center
    >
      <el-form :model="registerForm" ref="registerForm" label-width="80px">
        <el-form-item prop="villageName" label="村民姓名" size="mini">
          <el-input
            v-model="registerForm.villageName"
            placeholder="村名姓名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="townName" label="村镇姓名" size="mini">
          <el-input
            v-model="registerForm.townName"
            placeholder="村镇姓名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="cadreName" label="干部姓名" size="mini">
          <el-input
            v-model="registerForm.cadreName"
            placeholder="干部姓名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="time" label="留言时间" size="mini">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="registerForm.time"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="留言状态" size="mini">
          <el-radio-group v-model="registerForm.status">
            <el-radio :label="0">重要且紧急</el-radio>
            <el-radio :label="1">重要但不紧急</el-radio>
            <el-radio :label="2">不重要但基金</el-radio>
            <el-radio :label="3">不重要且不紧急</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="messageContent" label="留言内容" size="mini">
          <el-input
            v-model="registerForm.messageContent"
            placeholder="留言内容"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item label="解决状态" size="mini">
          <el-radio-group v-model="registerForm.resolutionStatus">
            <el-radio :label="0">未解决</el-radio>
            <el-radio :label="1">已解决</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="typeOfDifficulty" label="困难类型" size="mini">
          <el-cascader
            :options="options"
            v-model="registerForm.typeOfDifficulty"
            placeholder="困难类型"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="centerDialogVisible = false"
          >取消</el-button
        >
        <el-button size="mini" @click="addVillagersMessage">确定</el-button>
      </span>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog
      title="修改村民留言信息"
      :visible.sync="editVisible"
      width="400px"
      center
    >
      <el-form :model="form" ref="form" label-width="80px">
        <el-form-item prop="villageName" label="村民姓名" size="mini">
          <el-input
            v-model="form.villageName"
            placeholder="村名姓名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="townName" label="村镇姓名" size="mini">
          <el-input v-model="form.townName" placeholder="村镇姓名"></el-input>
        </el-form-item>
        <el-form-item prop="cadreName" label="干部姓名" size="mini">
          <el-input v-model="form.cadreName" placeholder="干部姓名"></el-input>
        </el-form-item>
        <el-form-item prop="time" label="留言时间" size="mini">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.time"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="留言状态" size="mini">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">重要且紧急</el-radio>
            <el-radio :label="1">重要但不紧急</el-radio>
            <el-radio :label="2">不重要但基金</el-radio>
            <el-radio :label="3">不重要且不紧急</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="messageContent" label="留言内容" size="mini">
          <el-input
            v-model="form.messageContent"
            placeholder="留言内容"
            type="textarea"
          ></el-input>
        </el-form-item>

        <el-form-item label="解决状态" size="mini">
          <el-radio-group v-model="form.resolutionStatus">
            <el-radio :label="0">未解决</el-radio>
            <el-radio :label="1">已解决</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="typeOfDifficulty" label="困难类型" size="mini">
          <el-cascader
            :options="options"
            v-model="form.typeOfDifficulty"
            placeholder="困难类型"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="editVIsible = false">取消</el-button>
        <el-button size="mini" @click="editVillagersMessage">确定</el-button>
      </span>
    </el-dialog>

    <!-- 删除 -->
    <el-dialog
      title="删除村民留言信息"
      :visible.sync="delVisible"
      width="300px"
      center
    >
      <div align="center">删除不可恢复，是否确定删除？</div>
      <span slot="footer">
        <el-button size="mini" @click="delVisible = false">取消</el-button>
        <el-button size="mini" @click="deleteRow">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getVillagersMessage,
  setVillagersMessage,
  updateVillagersMessage,
  deleteVillagersMessage,
} from "../api/index";
import { mixin } from "../mixins/index";
export default {
  mixins: [mixin],
  data() {
    return {
      options: [
        {
          value: "重点优抚对象",
          label: "重点优抚对象",
        },
        {
          value: "低保户",
          label: "低保户",
        },
        {
          value: "福利机构及散居",
          label: "福利机构及散居",
        },
        {
          value: "残疾",
          label: "残疾",
        },
        {
          value: "重大自然灾害造成",
          label: "重大自然灾害造成",
        },
        {
          value: "疾病",
          label: "疾病",
        },
      ],
      centerDialogVisible: false, //添加弹窗是否显示
      editVisible: false, //编辑弹窗是否显示
      delVisible: false, //删除弹窗是否显示
      tableData: [],
      tempData: [],
      select_word: "",
      pageSize: 5, //分页每页大小
      currentPage: 1, //当前页
      idx: -1, //当前选择项
      multipleSelection: [], //哪些项已经打勾
      // 添加框
      registerForm: {
        villageName: "",
        townName: "",
        cadreName: "",
        time: "",
        status: "",
        messageContent: "",
        resolutionStatus: "",
        typeOfDifficulty: "",
      },
      // 编辑框
      form: {
        id: "",
        villageName: "",
        townName: "",
        cadreName: "",
        time: "",
        status: "",
        messageContent: "",
        resolutionStatus: "",
        typeOfDifficulty: "",
      },
    };
  },
  computed: {
    //计算当前搜索结果表里的数据
    data() {
      return this.tableData.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      );
    },
  },
  watch: {
    //搜索框里面的内容发生变化的时候，搜索结果table列表的内容跟着它的内容发生变化
    select_word: function () {
      if (this.select_word === "") {
        this.tableData = this.tempData;
      } else {
        this.tableData = [];
        for (let item of this.tempData) {
          if (item.villageName.includes(this.select_word)) {
            this.tableData.push(item);
          }
        }
      }
    },
  },
  created() {
    this.getData();
  },
  methods: {
    //获取当前页
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    //查询所有村民留言
    getData() {
      this.tempData = [];
      this.tableData = [];
      getVillagersMessage().then((res) => {
        this.tempData = res;
        this.tableData = res;
        this.currentPage = 1;
      });
    },
    // 添加村民留言
    addVillagersMessage() {
      let d = this.registerForm.time;
      let datetime =
        d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
      let params = new URLSearchParams();
      params.append("villageName", this.registerForm.villageName);
      params.append("townName", this.registerForm.townName);
      params.append("cadreName", this.registerForm.cadreName);
      params.append("time", datetime);
      params.append("status", this.registerForm.status);
      params.append("messageContent", this.registerForm.messageContent);
      params.append("resolutionStatus", this.registerForm.resolutionStatus);
      params.append("typeOfDifficulty", this.registerForm.typeOfDifficulty);

      setVillagersMessage(params)
        .then((res) => {
          if (res.code === 1) {
            this.getData();
            this.notify("添加成功", "success");
            this.currentPage = 1;
          } else {
            this.notify("添加失败", "error");
          }
        })
        .catch((err) => {
          console.log(err);
        });
      this.centerDialogVisible = false;
    },
    //弹出编辑页面
    handleEdit(row) {
      this.editVisible = true;
      this.form = {
        id: row.id,
        villageName: row.villageName,
        townName: row.townName,
        cadreName: row.cadreName,
        time: row.time,
        status: row.status,
        messageContent: row.messageContent,
        resolutionStatus: row.resolutionStatus,
        typeOfDifficulty: row.typeOfDifficulty,
      };
    },
    //保存编辑页面修改的数据
    editVillagersMessage() {
      let d = new Date(this.form.time);
      let datetime =
        d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
      let params = new URLSearchParams();
      params.append("id", this.form.id);
      params.append("villageName", this.form.villageName);
      params.append("townName", this.form.townName);
      params.append("cadreName", this.form.cadreName);
      params.append("time", datetime);
      params.append("status", this.form.status);
      params.append("messageContent", this.form.messageContent);
      params.append("resolutionStatus", this.form.resolutionStatus);
      params.append("typeOfDifficulty", this.form.typeOfDifficulty);

      updateVillagersMessage(params)
        .then((res) => {
          if (res.code === 1) {
            this.getData();
            this.notify("修改成功", "success");
          } else {
            this.notify("修改失败", "error");
          }
        })
        .catch((err) => {
          console.log(err);
        });
      this.editVisible = false;
    },
    // 删除村民留言信息
    deleteRow() {
      deleteVillagersMessage(this.idx)
        .then((res) => {
          if (res) {
            this.getData();
            this.notify("删除成功", "success");
          } else {
            this.notify("删除失败", "error");
          }
        })
        .catch((err) => {
          console.log(err);
        });
      this.delVisible = false;
    },
  },
};
</script>
<style scoped>
.handle-box {
  margin-bottom: 20px;
}
.handle-input {
  width: 300px;
  display: inline-block;
}
.pagination {
  display: flex;
  justify-content: center;
}
</style>
